package com.hbue.springboot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbue.springboot.mapper.TbWorkerMapper;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.pojo.TbWorker;
import com.hbue.springboot.service.TbWorkerService;
import com.hbue.springboot.vo.QueryParamsVo;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;


@Service
@RequiredArgsConstructor
public class TbWorkerServiceImpl implements TbWorkerService {

    private final TbWorkerMapper tbWorkerMapper;

    private static final Logger log = LoggerFactory.getLogger(TbAccountServiceImpl.class);

    @Override
    public List<TbWorker> selectTick() {
        return tbWorkerMapper.selectTick();
    }

    /**
     * 根据ID调整送水工工资
     *
     * @param tbWorker 前端采集的送水工信息
     * @return true调整成功，false调整失败
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateWorkerSalaryById(TbWorker tbWorker) {
        Integer id = tbWorkerMapper.selectByName(tbWorker.getWorkerName());
        if(id==null){
            return -1;
        }
        tbWorker.setId(id);
        return tbWorkerMapper.updateById(tbWorker);
    }

    @Override
    public IPage<TbHistory> getOrderWithoutWorker(QueryParamsVo params) {
        log.info("list order without worker");
        Page<TbHistory> page = new Page<>(params.getPageNum(), params.getPageSize());
        IPage<TbHistory> list = tbWorkerMapper.selectOrderWithoutWorker(page);
        if(list == null) {
            return null;
        }
        return list;
    }

    @Override
    public IPage<TbHistory> getOrderWithWorker(QueryParamsVo params) {
        log.info("list order with worker ="+params);
        Page<TbHistory> page = new Page<>(params.getPageNum(), params.getPageSize());
        String name = (String) params.getParams().get("name");
        Integer id = tbWorkerMapper.selectByName(name);
        IPage<TbHistory> list = tbWorkerMapper.selectOrderWithWorker(page,id);
        if(list == null) {
            return null;
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer acceptOrder(Integer id, String workerName) {
        Integer workerId = tbWorkerMapper.selectByName(workerName);
        if(workerId == null) {
            return -1;
        }

        return tbWorkerMapper.updateWorkerId(id, workerId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer ensureOrder(Integer id) {
        Integer res = tbWorkerMapper.ensureOrder(id, new Date(System.currentTimeMillis()));
        if(res > 0) {
            return 1;
        } else {
            return -1;
        }
    }
}
